<?php
/**
 * Kornak
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.kornak-framework.org/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to contact@kornak-framework.org so we can send you a copy immediately.
 *
 * @category   Kornak
 * @package    Kornak_Iterator
 * @copyright  Copyright (c) 2008-2009 Kornak Group (http://www.kornak-framework.org/)
 * @version    $Id: $
 */

/**
 * Class to iterate over a CSV file.
 *
 * @category   Kornak
 * @package    Kornak_Iterator
 * @copyright  Copyright (c) 2008-2009 Kornak Group (http://www.kornak-framework.org/)
 */
class Kornak_Iterator_Csv implements Iterator
{
    protected $_currentLine;
    protected $_enclosure = '"';
    protected $_escape = '\\';
    protected $_delimiter = ',';
    protected $_fileHandle;
    protected $_lineLength = 0;
    protected $_path;
    protected $_index = 0;

    protected function _openFile($path)
    {
    	if (!is_readable($path)) {
    		require_once 'Kornak/Iterator/Csv/Exception.php';
    		throw new Kornak_Iterator_Csv_Exception("Could not open file '$path'");
    	}

        $this->_fileHandle = fopen($path, 'r');
    }

    public function __construct($path, $options = null)
    {
        $this->setPath($path);

    	if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function current()
    {
        return fgetcsv($this->_fileHandle,
                       $this->_lineLength,
                       $this->_delimiter,
                       $this->_enclosure,
                       $this->_escape);
    }

    public function getDelimiter()
    {
    	return $this->_delimiter;
    }

    public function getEnclosure()
    {
        return $this->_enclosure;
    }

    public function getEscape()
    {
        return $this->_escape;
    }

    public function getLineLength()
    {
        return $this->_lineLength;
    }

    public function getPath()
    {
        return $this->_path;
    }

    public function key()
    {
        return $this->_index;
    }

    public function next()
    {
        $this->_index++;
        next($this->_fileHandle);
    }

    public function rewind()
    {
        $this->_index = 0;
        rewind($this->_fileHandle);
    }

    public function setDelimiter($delimiter)
    {
        $this->_delimiter = $delimiter;
        return $this;
    }

    public function setEnclosure($enclosure)
    {
        $this->_enclosure = $enclosure;
        return $this;
    }

    public function setEscape($escape)
    {
        $this->_escape = $escape;
        return $this;
    }

    public function setLineLength($length)
    {
        $this->_lineLength = $length;
        return $this;
    }

    public function setOptions($options)
    {
        if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        } elseif (!is_array($options)) {
            require_once 'Zend/Layout/Exception.php';
            throw new Zend_Layout_Exception('setOptions() expects either an array or a Zend_Config object');
        }

        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }

        return $this;
    }

    public function setPath($path)
    {
    	$this->_path = $path;
    	$this->_openFile($path);
        return $this;
    }

    public function valid()
    {
        return !feof($this->_fileHandle);
    }
}